package com.simplesoftwares.client.library.appstructure;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.itextpdf.text.log.SysoCounter;
import com.simplesoftwares.client.library.HasTable;
import com.simplesoftwares.client.library.appstructure.formtablescreen.FormTableScreen;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;

/**
 * This class creates a pop view for search functionality.Objects of this class are responsible for displaying 
 * the view when search is clicked.
 * To Do: Naturally master entity of this class is MyQuerry.Change as per.
 * @author Kamla
 * @param <T>
 */
public abstract class SearchPopUpScreen<T> extends FormTableScreen<T> implements HasTable{
	
	/**
	 * popup panel on which search ui goes.
	 */
	protected PopupPanel popup;
	/**
	 * download on click of this label  the table data is downloaded in xl format.
	 * To Do : change type in widget so that we can use icons in future
	 */
	protected InlineLabel dwnload;
	/**
	 *  on click of this label  the search table fills.
	 *  To Do : change type in widget so that we can use icons in future
	 * 
	 */
	protected InlineLabel golbl;
	/**
	 * Horizontal panle to hold go and download
	 */
	public HorizontalPanel horizontal;
	/**
	 * Default height of table
	 */
	private String DEFAULT_HEIGHT="350px";
	/**
	 * This attribute decides weather the result table is on pop up or on form.
	 */
	public  boolean TABLE_ON_POPUP=true;
	
	/**
	 * Used to set table without object creation.Mostly a hack.
	 */
	public static Object staticSuperTable;
	
   /**
    * Instatiate a new search pop up
    * To Do : Komal change the constructors as in date box implementation
    * @param table the table on which result will be plased.It can be on form on pop up.
    */
	public SearchPopUpScreen(SuperTable<T> table)
	{
		super(table);
		horizontal=new HorizontalPanel();
		popup=new PopupPanel(true);
		golbl=new InlineLabel("Go");
		dwnload=new InlineLabel("Download");
	}
	
	//An elegent hack
	
	public SearchPopUpScreen()
	{
		this((SuperTable<T>) staticSuperTable);
		
	}
	
	public SearchPopUpScreen(boolean mode)
	{
		this((SuperTable<T>) staticSuperTable,mode);
		
	}
	
	
	/**
	 * Instantiate with table and mode
	 * @param table the table on which result will stick
	 * @param mode mode which decides table will stick on pop up or not.
	 */
	
	public SearchPopUpScreen(SuperTable<T> table,boolean mode)
	{
		this(table);
		TABLE_ON_POPUP=mode;
	}
/////////////////////////////////////////////////ABSTRACT METHODS///////////////////////////////////////////////////
	
	
	/**
	 * The method is implemented by concrete Which will crete a {@link MyQuerry} object corresponding to SearchPopUp
	 * @return {@link MyQuerry} object
	 */
	public abstract MyQuerry getQuerry();
	
///////////////////////////////////////////////////CSS STYLING//////////////////////////////////////////////////////
	
	/**
	 * 
	 * @param w applies the style on widget
	 */
	protected void setStyling(Widget w)
	{
		w.getElement().setClassName("tablecaption");
		
	}

	/**
	 * Sets the styling
	 */
	
	@Override
	public void applyStyle() {
	
		content.getElement().setId("formcontent");
		form.getElement().setId("form");
		horizontal.getElement().addClassName("centering");
		dwnload.getElement().setId("addbutton");
		golbl.getElement().setId("addbutton");
		
		popup.getElement().setId("searchpopup");
		supertable.getTable().setHeight("400px");
		scrollpanel.setHeight("500px");
		popup.center();
		
	}
	
///////////////////////////////////////////GUI CREATION//////////////////////////////////////////////////
	//Idealy we should have passed the table and form should have been created as pop up is Form Table Screen
	//This shows limitation of design as no of ui elements to be added is fixed.
	
	/**
	 * Sticks Form or FormTable content on popup.
	 */
	
	@Override
    public void createGui()
	{
		content.add(form);
		horizontal.add(golbl);
		horizontal.add(dwnload);
		content.add(horizontal);
		if(TABLE_ON_POPUP)
			addTable();
		
		applyStyle();
		
		
		popup.add(content);
		popup.hide();
		
	}

///////////////////////////////////////POPUP RELATED METHODS/////////////////////////////////////////////// 
	/**
	 * shows the search popup.
	 * 
	 */
	public void showPopUp()
	{
		clear();
		popup.show();
		popup.setSize("1150px","600px");
		popup.center();
		popup.setAnimationEnabled(true);
		
		
	}
	
	/**
	 * Hides the Popn Up
	 */
	public void hidePopUp()
	{
		popup.hide();
	}
	
/////////////////////////////////////////APPLYING HANDLER ON "GO" & "DOWNLOAD" LABELS///////////////////////
	
	/**
	 * Applies handlers on go and download widgets
	 * @param handler handler object which will handel click events on these widgets
	 */
	public void applyHandler(ClickHandler handler)
	{
		golbl.addClickHandler(handler);
		dwnload.addClickHandler(handler);
	}
	
	@Override
	public void clear()
	{
		super.clear();
		if(supertable!=null&&TABLE_ON_POPUP==true)
			supertable.clear();
	}
	
	



	///////////////////////////////////////////////////////GETTER SETTERS///////////////////////////////////////////
	public PopupPanel getPopup() {
		return popup;
	}

	public void setPopup(PopupPanel popup) {
		this.popup = popup;
	}

	public InlineLabel getDwnload() {
		return dwnload;
	}

	public void setDwnload(InlineLabel dwnload) {
		this.dwnload = dwnload;
	}

	public InlineLabel getGolbl() {
		return golbl;
	}

	public void setGolbl(InlineLabel golbl) {
		this.golbl = golbl;
	}

	public HorizontalPanel getHorizontal() {
		return horizontal;
	}

	public void setHorizontal(HorizontalPanel horizontal) {
		this.horizontal = horizontal;
	}

	

	@Override
	public void toggleAppHeaderBarMenu() {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void updateModel(T model) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateView(T view) {
		// TODO Auto-generated method stub
		
	}
	
	
	
  
}
